class Solution {
public:
    int maxSumDivThree(vector<int>& nums) {
        //贪心做法  先加再减
        int sum=0;
        long long first_1=INT_MAX,second_1=INT_MAX;
        long long first_2=INT_MAX,second_2=INT_MAX;
        for(auto e:nums)
        {
            sum+=e;
            if(e%3==1)
            {
                if(e<first_1)
                {
                    second_1=first_1;

                    first_1=e;
                }
                else if(e<second_1)
                {
                    second_1=e;
                }
            }
            else if(e%3==2)
            {
                if(e<first_2)
                {
                    second_2=first_2;

                    first_2=e;
                }
                else if(e<second_2)
                {
                    second_2=e;
                }

            }

        }
        if(sum%3==0) return sum;
        else if(sum%3==1)
        {

            return max(sum-first_1,sum-first_2-second_2);
        }
        else 
        {


            return max(sum-first_2,sum-first_1-second_1);

        }

    }
};